<?php
namespace app\admin\controller;
use think\Controller;

Class Category extends BaseAdmin
{

	//栏目列表
    public function lst()
    {
        
        $obj=db('tp_category');
        if(request()->isPost()){
            $data0=input('post.');
            $this->CateTree->cateSort($data0['sort'],$obj);
            $this->success('排序成功', url('category/lst'));
        
        }   
    	
        //无限极分类
        $data=$obj->order('sort DESC')->select();


        $data=$this->CateTree->catetree($data);
        // dump($data);
        // die;
        $this->assign('data',$data);

        return $this->fetch('list');

    }



    //商品分类添加
    public function add()
    {
       
        $obj=model('TpCategory');
        $data=$obj->order('sort DESC')->select();
        $data=$this->CateTree->catetree($data);

         //推荐位信息
        $CategoryRecposRes=db('tp_recpos')->where(['rec_type'=>2])->select();

        //商品类型信息
        $typeRes=db('tp_type')->select();

        $this->assign([
            'data'=>$data,
            'CategoryRecposRes'=>$CategoryRecposRes,
            'typeRes'=>$typeRes
        ]);


        if(request()->isPost()){
        	$data=input('post.');
            // dump($data);die;
            if($_FILES['cate_img']['tmp_name']){
                $data['cate_img']=$this->upload();
            }


        	//验证
        	// $validate = new \app\admin\validate\category;
        	// if (!$validate->check($data)) {
        	// 	$this->error($validate->getError());
        	// }
        	
        	$add=$obj->save($data);
        	if($add){
        		$this->success('保存成功','lst');
        	}
        	else{
        		$this->error('保存失败');
        	}

        }
        return $this->fetch();

    }



    //商品分类编辑
    public function edit($id)
    {
   
        $obj=model('TpCategory');;
        $data=$obj->order('sort DESC')->select();
        $data=$this->CateTree->catetree($data);

        $list=$obj->where(array('id'=>$id))->find();

         //推荐位信息
        $CategoryRecposRes=db('tp_recpos')->where(['rec_type'=>2])->select();
        //商品推荐位内容
        $_myCategoryRecposRes=db('tp_rec_item')->where(['value_type'=>2,'value_id'=>$id])->select();
        $myCategoryRecposRes=array();
        foreach ($_myCategoryRecposRes as $k => $v) {
            $myCategoryRecposRes[]=$v['recpos_id'];
        }
        // dump($myCategoryRecposRes);die;

         //商品类型信息
        $typeRes=db('tp_type')->select();

        //筛选属性的id
        $searchAttrIds=$obj->field('search_attr_ids')->where('id',$id)->find();
        $map=array();
        $map['id']=['in',$searchAttrIds['search_attr_ids']];
        $attrRes=db('tp_attr')->where($map)->select();
        // dump($attrRes);die;

        $this->assign(
            [
                'data'=>$data,
                'list'=>$list,
                'CategoryRecposRes'=>$CategoryRecposRes,
                'myCategoryRecposRes'=>$myCategoryRecposRes,
                 'typeRes'=>$typeRes,
                 'attrRes'=>$attrRes
            ]
        );


    	if(request()->isPost()){
        	
            $data=input('post.');
            // dump($data);die;

            if($_FILES['cate_img']['tmp_name']){
                if($list['cate_img']){
                    $img=IMG_UPLOADS.$list['cate_img'];
                    if(file_exists($img)){
                        @unlink($img);
                    }
                }                    
                 $data['cate_img']=$this->upload();

            }
                        

            $up=$obj->update($data);
        	
        	if($up!==false){
        		$this->success('修改成功','lst');
        	}
        	else{
        		$this->error('修改失败');
        	}

        }
        return $this->fetch();
    }



    //商品分类删除(删除上级栏目，所有下级栏目都要删除)
    public function del($id)
    {   
        //栏目下所有子栏目id
        $category=db('tp_category');
        $sonids=$this->CateTree->childrenids($id,$category);
        $sonids[]=intval($id);

    
        foreach ($sonids as $k => $v) {
            //删除所有子栏目的缩略图
            $list[]=$category->field('cate_img')->where(array('id'=>$v))->find();

            //删除推荐位关联信息
            db('tp_rec_item')->where(array('value_id'=>$v,'value_type'=>2))->delete();


        }
        
        foreach ($list as $k => $v) {
            if($v['cate_img']){
                $img=IMG_UPLOADS.$v['cate_img'];
                if(file_exists($img)){
                    @unlink($img);

                }
            }
        }
        
        
        //删除子栏目下的文章和文章缩略图
        // $article=db('tp_article');
        // foreach ($sonids as $k => $v) {
        //    $ArtRes=$article->field('id,thumb')->where(array('category_id'=>$v))->select();
        //    foreach ($ArtRes as $k1 => $v1) {
        //     $ARtImg=IMG_UPLOADS.$v1['thumb'];
        //         if(file_exists($ARtImg)){
        //             @unlink($ARtImg);
        //         }
        //         $article->delete($v1['id']);
        //     }

        // }

    	$del=$category->delete($sonids);
    	if($del){
    		$this->success('删除成功','lst');

    	}else{
            $this->error('删除失败');
    	}
    	
        return $this->fetch();
    }





    //图片上传
    public function upload(){
        $file=request()->file('cate_img');
        if($file==null){
            exit(json_encode(array('msg'=>'没有文件上传')));
        }
        $info=$file->move('../public/static/uploads');
        $ext=$info->getExtension();//获取后缀名
        if(!in_array($ext,array('jpg','png','gif','jpeg'))){
            exit(json_encode(array('msg'=>'文件格式不符合')));
        }
        if($info){
            return $img=$info->getSaveName();

        }else{
            $file->getError();
            die;
        }
        
        
    }





}